﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Web.Views.SystemLogin
{
    partial class GetCommanMenuById : PageBase
    {
        private IMDataHelper dal = null; 
        private string loadingUrl = "Views/SystemLogin/loadingpage.aspx";
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            //获得父ID
            dal = new IMDataHelper();
            string _tId = this.Request.QueryString["Id"] == null ? string.Empty : this.Request.QueryString["Id"];
            //获取子节点的json信息
            string sjson = GetNavigateMenuData(_tId);
            if (!string.IsNullOrEmpty(sjson))
            {
                this.Response.Write(sjson);
            }
        }


        /// <summary>
        /// 根据父ID获取子节点信息
        /// </summary>
        /// <param name="parent_Id"></param>
        /// <returns></returns>
        public string GetNavigateMenuData(string _Id)
        {
            string sql = "  SELECT a.Menu_ID  menuId,a.Parent_Menu_ID  parentMenuId,a.Menu_URL  menuUrl,a.Order_Num  menuOrder,b.vDesc  menuText,b.vCulture FROM P_Auth_Menu  a join (select * from Config_Res_System where vStringKey in (SELECT Menu_ID FROM P_Auth_Menu))  b on a.MENU_ID =b.vStringKey where b.vPageUrl = 'Public/Modules' and vCulture='zh-CN' and a.Menu_ID like '%" + _Id + "%'";
            sql = sql + " order by Order_Num desc";
            DataTable dt = dal.DBHelper.ExecuteDataSet(sql).Tables[0];
            string sresult = "";
            if (dt.Rows.Count > 0)
            {
                DataTable dtCopy = dt.Clone();
                foreach (DataRow row in dt.Rows)
                {
                    string Id = row["menuId"].ToString();
                    string _menuUrl = row["menuUrl"].ToString();
                    if (_menuUrl != string.Empty)
                    {
                        //判断权限
                        bool hasAuthority = (this.HasAuthority("ViewAll", Id) ||
                           this.HasAuthority("ViewDeptAll", Id) ||
                           this.HasAuthority("ViewDept", Id) ||
                           this.HasAuthority("ViewCreate", Id));
                        //如果没有权限，则删除复制的表中的该字段
                        if (hasAuthority)
                        {
                            DataRow ImpRow = dt.Select("menuId='" + Id + "'")[0];
                            //dtCopy.Rows.Remove(deleteRow);
                            //dtCopy.
                            dtCopy.ImportRow(ImpRow);
                        }
                    }
                    //如果是没有Url 的，判断下面是否有子节点，有的话 就不删除
                    else
                    {
                        //找出子节点，如果子节点删除光了，则删除这个节点
                        DataRow[] rowsClild = dtCopy.Select("parentMenuId='" + Id + "'");
                        if (rowsClild.Length > 0)
                        {
                            DataRow ImpRow = dt.Select("menuId='" + Id + "'")[0];
                            dtCopy.ImportRow(ImpRow);
                        }
                    }

                }
                //做完这些操作，再组织数据
                string Menu_Url = "";
                sresult += "[";
                //循环剩下的表
                //加上首页作为根节点
                //选出首页,如果有的话
                DataRow[] rowP = dt.Select("menuId='ePlanet'");
                if (rowP.Length > 0)
                {
                    rowP[0]["parentMenuId"] = "0";
                    rowP[0]["menuUrl"] = "";
                    dtCopy.ImportRow(rowP[0]);
                }
                DataView dv = dtCopy.DefaultView;
                    dv.Sort = "menuOrder asc";
                dtCopy = dv.ToTable();
                foreach (DataRow item in dtCopy.Rows)
                {
                    string nodeId = item["menuId"].ToString();
                    //判断父节点是否存在，如果不存在，作为根节点之一
                    //string pid = (item["parentMenuId"].ToString() == string.Empty) ? "0" : item["parentMenuId"].ToString();
                    DataRow[] rows = dt.Select("menuId='" + item["parentMenuId"].ToString() + "'");
                    string pid = "";
                    if (rows.Length == 0) //如果是根节，根节点不判断权限
                    {
                        pid = "0";
                    }
                    else
                    {
                        pid = item["parentMenuId"].ToString();
                    }
                    //        //对Url进行修改
                    if (item["menuUrl"].ToString().ToLower() == "desktop.aspx")
                    {
                        Menu_Url = "" + loadingUrl + "?Menu_URL=" + HttpUtility.UrlEncode(item["menuUrl"].ToString());
                    }
                    else if (item["menuUrl"].ToString().Length == 0)
                    {
                        Menu_Url = "";
                    }
                    else
                    {
                        Menu_Url = "" + loadingUrl + "?Menu_URL=../../" + HttpUtility.UrlEncode(item["menuUrl"].ToString());
                    }
                    string menuId = item["menuId"].ToString().Trim();
                    if (menuId != "ePlanet.SFDocs.WFDocsHistory" && menuId != "ePlanet.SFDocs.ReceiveUser" && menuId != "ePlanet.SFDocs.GetRFDeal" && menuId != "ePlanet.RFDocs.ReceiveUser" && menuId != "ePlanet.RFDocs.History" && menuId != "ePlanet.FullTextRetrieval.SFViews" && menuId != "ePlanet.FullTextRetrieval.RFViews")
                    {
                        nodeId = nodeId.Replace(".", "_");
                        pid = pid.Replace(".", "_");
                        //组织Json对象
                        sresult += "{";
                        sresult += " \"id\": \"" + nodeId + "\",\"pid\":\"" + pid + "\",\"text\": \"" + item["menuText"].ToString() + "\",'attribute':[{'url':'" + Menu_Url + "'}]";
                        sresult += "},";
                    }
                }
                if (sresult.Length > 1)
                {
                    sresult = sresult.Substring(0, sresult.Length - 1);
                }
                sresult += "]";
            }
            return sresult;
        }
            //    string Menu_Url = "";
            //    sresult += "[";
            //    foreach (DataRow item in dt.Rows)
            //    {
            //        string nodeId = item["menuId"].ToString();                  
            //        //判断父节点是否存在，如果不存在，作为根节点之一
            //        //string pid = (item["parentMenuId"].ToString() == string.Empty) ? "0" : item["parentMenuId"].ToString();
            //        DataRow[] rows = dt.Select("menuId='" + item["parentMenuId"].ToString() + "'");
            //        string pid = "";
            //        if (rows.Length == 0) //如果是根节，根节点不判断权限
            //        {
            //            pid = "0";
            //        }
            //        else
            //        {
            //            pid = item["parentMenuId"].ToString();
            //            //对于父节点不为空,有效链接节点的节点判断权限
            //            if (item["menuUrl"].ToString() != string.Empty)
            //            {
            //                bool hasAuthority = (this.HasAuthority("ViewAll", nodeId) ||
            //               this.HasAuthority("ViewDeptAll", nodeId) ||
            //               this.HasAuthority("ViewDept", nodeId) ||
            //               this.HasAuthority("ViewCreate", nodeId));
            //                //如果没有权限，则跳过这个节点
            //                if (!hasAuthority)
            //                {
                                
            //                }
            //            }
            //            else//对于父节点不为空的节点，则判断一下该节点下有Url
            //            {
            //            }
            //        }
            //        //对Url进行修改
            //        if (item["menuUrl"].ToString().ToLower() == "desktop.aspx")
            //        {
            //            Menu_Url = "" + loadingUrl + "?Menu_URL=" + HttpUtility.UrlEncode(item["menuUrl"].ToString());
            //        }
            //        else if (item["menuUrl"].ToString().Length == 0)
            //        {
            //            Menu_Url = "";
            //        }
            //        else
            //        {
            //            Menu_Url = "" + loadingUrl + "?Menu_URL=../../" + HttpUtility.UrlEncode(item["menuUrl"].ToString());
            //        }
            //        string menuId = item["menuId"].ToString().Trim();
            //        if (menuId != "ePlanet.SFDocs.WFDocsHistory" && menuId != "ePlanet.SFDocs.ReceiveUser" && menuId != "ePlanet.SFDocs.GetRFDeal" && menuId != "ePlanet.RFDocs.ReceiveUser" && menuId != "ePlanet.RFDocs.History" && menuId != "ePlanet.FullTextRetrieval.SFViews" && menuId != "ePlanet.FullTextRetrieval.RFViews")
            //        {
            //            nodeId = nodeId.Replace(".", "_");
            //            pid = pid.Replace(".", "_");
            //            //组织Json对象
            //            sresult += "{";
            //            sresult += " \"id\": \"" + nodeId + "\",\"pid\":\"" + pid + "\",\"text\": \"" + item["menuText"].ToString() + "\",'attribute':[{'url':'" + Menu_Url + "'}]";
            //            sresult += "},";
            //        }
            //    }
            //    sresult = sresult.Substring(0, sresult.Length - 1);
            //    sresult += "]";
            //}
            //return sresult;
        //} 

        /// <summary>
        /// 判断是否是叶节点
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        public bool IsLeafNode(string nodeId)
        {
            string sql = "select count(*) from P_Auth_Menu where Parent_Menu_ID='" + nodeId + "'";
            object obj = dal.DBHelper.ExecuteScalar(sql);
            if ((int)obj > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
